package com.htfyun.ft.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

/**
 * Created by sst on 2018/5/21.
 */

public class FileReadWrite {

    private final static String TAG = "FileReadWrite";

    public synchronized static void writeStringToFile(String filePath, String content) {
        File file = new File(filePath);
        Writer writer = null;

        try {
            writer = new OutputStreamWriter(new FileOutputStream(file));
            writer.write(content);
            writer.flush();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

    public synchronized static String readFileByLines(File file) {

        if (file == null || !file.exists()) {
            return "";
        }

        BufferedReader reader = null;
        StringBuilder stringBuilder = new StringBuilder("");

        try {
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
                stringBuilder.append(tempString);
                stringBuilder.append("\n");
            }

        } catch (IOException e) {

            e.printStackTrace();

        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }

        return stringBuilder.toString();
    }

    public synchronized static String readFileByLines(String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            return "";
        }
        BufferedReader reader = null;
        StringBuilder stringBuilder = new StringBuilder("");

        try {
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
                stringBuilder.append(tempString);
            }

        } catch (IOException e) {

            e.printStackTrace();

        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }

        return stringBuilder.toString();
    }

    public synchronized static String readFileByLinesWithReplace(String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            return "";
        }
        BufferedReader reader = null;
        StringBuilder stringBuilder = new StringBuilder("");

        try {
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
                stringBuilder.append(tempString.replace(" ", "").replace("\n", ""));
            }

        } catch (IOException e) {

            e.printStackTrace();

        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }

        return stringBuilder.toString();
    }

    /**
     * 追加文件：使用FileWriter
     *
     * @param fileName
     * @param content
     */
    public static void writeAppendFile(String fileName, String content) {

        File file = new File(fileName);
        if (!file.exists()) {
            try {
                if (file.getParentFile() != null && !file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }

                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        FileWriter writer = null;

        try {
            writer = new FileWriter(fileName, true);

            // 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
            writer.write(content);
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

}
